import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.Body;
import pulpcore.CoreSystem;
import pulpcore.Stage;
import pulpcore.sound.Sound;
import pulpcore.animation.Easing;
import pulpcore.animation.Timeline;
import pulpcore.animation.event.TimelineEvent;
import pulpcore.image.Colors;
import pulpcore.image.CoreFont;
import pulpcore.image.CoreImage;
import pulpcore.scene.Scene;
import pulpcore.scene.Scene2D;
import pulpcore.sprite.Button;
import pulpcore.sprite.FilledSprite;
import pulpcore.sprite.Group;
import pulpcore.sprite.Sprite;
import pulpfizz.physics.BodyUtils;
import pulpfizz.physics.Material;
import pulpfizz.pulp.body.Java2DBodySprite;

/**
 * Created by will
 * Date: May 25, 2008 9:53:55 PM
 */
public class MainMenuScene extends Scene2D {
Arena physics;
Group buttons = new Group();
    private Button play;
    private Button options;
    private Button about;
    private FilledSprite fadeSprite;
    private pulpcore.sprite.Label pelter;
    private Button jordanbros;
    private Button muteButton;

    public void load() {
        setupWorld();

        plop = Sound.load("plop2.ogg");
        
        muteButton = new Button(CoreImage.load("mute.png").split(3,2), Stage.getWidth()-10, Stage.getHeight()-10, true);
        muteButton.setSelected(!CoreSystem.isMute());
        muteButton.setPixelLevelChecks(false);
        muteButton.setAnchor(Sprite.SOUTH_EAST);
        add(muteButton);

        CoreFont font = CoreFont.load("digital.font.png").scale(1.5);
        pelter = new pulpcore.sprite.Label(font,"PELTER", Stage.getWidth()/2,Stage.getHeight()/4);
        pelter.setAnchor(Sprite.CENTER);
        add(pelter);
        pelter.alpha.set(0);
        pelter.alpha.animate(0,255,500,Easing.REGULAR_IN_OUT,500);
        pelter.scale(0,0, pelter.width.get(), pelter.height.get(),1000, Easing.ELASTIC_OUT,500);

        final pulpcore.sprite.Label by = new pulpcore.sprite.Label(CoreFont.getSystemFont(),"by Will Jordan",Stage.getWidth()/2,Stage.getHeight()/4 + 30);
        by.setAnchor(Sprite.CENTER);
        buttons.add(by);

        play = Button.createLabeledButton("Play game!", Stage.getWidth()/2,Stage.getHeight()/2);
        int h = (int) play.height.get() * 2;
        play.setAnchor(Sprite.CENTER);
        buttons.add(play);
//        options = Button.createLabeledButton("Options", Stage.getWidth()/2,Stage.getHeight()/2+h);
//        options.setAnchor(Sprite.CENTER);
//        buttons.add(options);
        about = Button.createLabeledButton("About", Stage.getWidth()/2,Stage.getHeight()/2+h);
        about.setAnchor(Sprite.CENTER);
        buttons.add(about);
        add(buttons);
        buttons.alpha.set(0);
        buttons.alpha.animate(0,255,500,Easing.REGULAR_IN_OUT,500);

        CoreImage sjb = CoreImage.load("jordanbros.png");
        CoreImage[] images = {sjb,sjb,sjb};
        jordanbros = new Button(images, Stage.getWidth() - 42,Stage.getHeight()-42);
        jordanbros.setAnchor(Sprite.SOUTH_EAST);
        buttons.add(jordanbros);

        fadeSprite = new FilledSprite(Colors.rgba(Colors.BLACK,255));
        add(fadeSprite);
        fadeSprite.alpha.animate(255,0,500);
    }

    private void setupWorld() {
        physics = new Arena(20);
        physics.setAnchor(Sprite.CENTER);
        add(physics);
        physics.fitToStage();
        physics.scores.visible.set(false);
        physics.setGravity(new Vec2(0,0));

        // World container.
        Body body = BodyUtils.createHollowBox(physics.getWorld(), 0, 0, 40, 30, 2f);
        Material.DEAD_WEIGHT().apply(body);
        physics.add(new Java2DBodySprite(body, physics, 1, java.awt.Color.DARK_GRAY, java.awt.Color.black, java.awt.Color.black));
    }


    public void buttonSelect(Button b, final Scene nextScene) {
        Timeline t = new Timeline();
        for(int i=0;i<buttons.getNumSprites();i++) {
            Sprite s = buttons.get(i);
            t.animateTo(s.alpha,0,500);
        }
        b.scaleTo(0,0,500,Easing.STRONG_IN_OUT);
        t.addEvent(new TimelineEvent(500) { public void run() {
                Stage.setScene(nextScene);
            }});
        addTimeline(t);
    }

    Sound plop;
    public void update(int elapsedTime) {
        if(play.isClicked()) {
            buttonSelect(play,new Pulptron());
            pelter.scaleTo(0,0,500,Easing.BACK_IN);
            plop.play();
        }
        if(about.isClicked()) {
            Stage.pushScene(new AboutScene());
            plop.play();
        }
        if(jordanbros.isClicked()) {
            CoreSystem.showDocument("http://www.gamefight.org");
            plop.play();
        }
        if (muteButton.isClicked()) {
            CoreSystem.setMute(!muteButton.isSelected());
            plop.play();
        }
    }
}
